package com.ali.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger("TestController");

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public ResponseEntity<Map<String, String>> get(@RequestHeader Map<String, Object> header) {
        logger.info("GET BEGIN");

        logger.info("get header：" + header);

        logger.info("GET END");

        return getResponseEntity("Hello Get");
    }

    private ResponseEntity<Map<String, String>> getResponseEntity(String message) {
        HttpHeaders headers = new HttpHeaders();
        headers.add("Header1", "Header1");
        headers.add("Header2", "Header2");

        Map<String, String> param = new HashMap<>();
        param.put("message", message);

        ResponseEntity.BodyBuilder bodyBuilder = ResponseEntity.status(200);
        return bodyBuilder.headers(headers).body(param);
    }

    @RequestMapping(value = "/post", method = RequestMethod.POST)
    public ResponseEntity<Map<String, String>> post(@RequestBody String request, @RequestHeader Map<String, Object> header) {
        logger.info("POST BEGIN");

        logger.info("post request：" + request);

        logger.info("post header：" + header);

        logger.info("POST END");

        return getResponseEntity("Hello Post");
    }
}